package com.odm.communitymanage.ui.help

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.AllSeekHelp
import com.odm.communitymanage.core.bean.SeekHelpData
import com.odm.communitymanage.core.net.ApiService
import com.rxjava.rxlife.lifeOnMain
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_help_list.*
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 求助列表页面
 * @author: ODM
 * @date: 2020/4/14
 */
class HelpListFragment : BaseFragment(){

    var disposable : Disposable ?= null
    private var rvAdapter : SeekHelpAdapter ?= null



    override fun onSupportVisible() {
        super.onSupportVisible()
        //页面可见时，获取数据
        getHelpListData()

    }

    override fun onDestroyView() {
        super.onDestroyView()
        //销毁请求
        if(disposable?.isDisposed == false){
            disposable?.dispose()
        }
    }

    override fun initViews() {
        //发布求助 点击事件
        tvPostHelp.setOnClickListener {
            start(PostHelpFragment())
        }
        //左上角返回事件
        btnBack_HelpList.setOnClickListener {
            pop()
        }

        initHelpListRecyclerView()

    }

    private fun initHelpListRecyclerView(){
        rvHelpList.layoutManager = LinearLayoutManager(requireContext())
        rvAdapter = SeekHelpAdapter(mutableListOf())
        rvHelpList.adapter  = rvAdapter
        //设置空布局
        rvAdapter?.setEmptyView(R.layout.layout_empty_list)
        rvAdapter?.setOnItemClickListener { adapter, view, position ->
            LiveEventBus
                .get(Constants.EVENT_HELP_DETAIL)
                .post(rvAdapter?.data?.get(position))
            start(HelpDetailFragment())
        }

    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_help_list
    }

    private fun getHelpListData(){
        disposable = RxHttp.get(ApiService.ALL_SEEK_HELP)
            .asClass(AllSeekHelp::class.java)
            .lifeOnMain(this)
            .subscribe({
                                if(it.code == 1){
                                    //成功刷新
                                    val dataList : MutableList<SeekHelpData> =
                                        it.data.reversed().toMutableList()
                                    rvAdapter?.setNewData(dataList)
                                }else {
                                    ToastUtils.showShort(it.message)
                                }
                                },{
                                 ToastUtils.showShort(it.message)
                             })


    }


}